package com.arunsawad.ilertu.activity;

import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.arunsawad.ilertu.R;
import com.arunsawad.ilertu.common.AsyncActivity;
import com.arunsawad.ilertu.common.Constants;
import com.arunsawad.ilertu.common.LoadImageTask;
import com.arunsawad.ilertu.common.RequestTask;
import com.arunsawad.ilertu.common.Validator;

public class ServiceDetails extends Activity implements AsyncActivity {
	SharedPreferences preferences;
	ProgressDialog bar;
	String serviceId = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_servicedetails);

		preferences = getSharedPreferences(Constants.PREF_FILE, MODE_PRIVATE);
		Validator.Validate(this, preferences);
		
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			serviceId = extras.getString("serviceId");
		}
		LoadContent();
	}

	public void LoadContent() {
		// show progress bar
		bar = new ProgressDialog(ServiceDetails.this);
		bar.setIndeterminate(true);
		bar.show();

		String filter = "<service id=\"" + serviceId + "\"/>";
		String msg = String.format(Constants.MSG, "ilertu.user.services-get",
				preferences.getString(Constants.PREF_USER_NAME, ""),
				preferences.getString(Constants.PREF_PASSWORD, ""),
				preferences.getString(Constants.PREF_LANGUAGE, "en"), filter);

		RequestTask task = new RequestTask();
		task.setListener(this);
		task.execute(Constants.URL_GET_REGISTERED_SERVICE, msg);
	}

	@Override
	public void onRequestCompleted(String results) {
		if (results != null) {
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder builder;
			try {
				builder = factory.newDocumentBuilder();
				Document document = builder.parse(new InputSource(
						new StringReader(results)));
				NodeList nodes = document.getElementsByTagName("service");

				if (nodes.getLength() > 0) {
					Element e = (Element) nodes.item(0);
					String name = e.getElementsByTagName("name").item(0)
							.getTextContent();
					String desc = e.getElementsByTagName("desc").item(0)
							.getTextContent();
					String imageUrl = e.getElementsByTagName("imageUrl")
							.item(0).getTextContent();
					String status = e.getElementsByTagName("responseStatus")
							.item(0).getTextContent();

					TextView txtViewName;
					TextView txtViewDesc;
					TextView txtViewStatus;

					txtViewName = (TextView) findViewById(R.id.servicedetails_txtViewName);
					txtViewName.setText(name);
					txtViewDesc = (TextView) findViewById(R.id.servicedetails_txtViewDesc);
					txtViewDesc.setText(desc);
					if (status.equals("0")) {
						status = getString(R.string.status_waiting);
					} else {
						if (status.equals("1")) {
							status = getString(R.string.status_accepted);
						} else {
							status = getString(R.string.status_rejected);
						}
					}
					txtViewStatus = (TextView) findViewById(R.id.servicedetails_txtViewStatus);
					txtViewStatus.setText(status);

					ImageView serviceImage = (ImageView) findViewById(R.id.servicedetails_image);
					serviceImage.setTag(imageUrl);
					LoadImageTask loadImageTask = new LoadImageTask();
					loadImageTask.execute(serviceImage);
				}
				// hide progress bar
				bar.hide();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}